<?php if (defined("YTDJ") or die()); 

Class Controller {
	
	/**
	 * Template to be used by this module
	 *
	 * @var string
	 */
	var $tmpl;
	
	/**
	 * View to be used by this module
	 *
	 * @var string
	 */
	var $view;
	
	/**
	 * Same as main class task
	 *
	 * @var string
	 */
	var $task;
	
	/**
	 * Constructor
	 *
	 */
	function __construct($module, $task) {
		$this->view = $module;
		$this->task = $task;
	}
	
	/**
	 * Displays current view
	 *
	 * @param string $view
	 * @return string
	 */
	function display($view = NULL) {

		// use custom view?
		if (!$view) {
			if ($this->task) {
				$view = $this->task;
			} else {
				$view = $this->view;
			}
		}
			
		// include template
		$viewfile = "modules/$this->view/views/$view.view.php";		
		if (file_exists($viewfile)) {
			
			// get view result
			ob_start();
			include($viewfile);
			$viewcontent = ob_get_clean();
			ob_end_clean();
			
			// loads view in template
			if ($this->tmpl) {
				$echo = str_replace("###___CONTENT___###", $viewcontent, $this->tmpl);
			} else {
				$echo = $viewcontent;
			}
			
			// echo out view and template			
			Main::write($echo);
		} else {
			Main::error("No view found");
		}
	}
	
	/**
	 * Loads current template
	 *
	 * @param string $tmpl
	 */
	function loadTemplate($tmpl = NULL) {

		// use custom template?
		if (!$tmpl) {
			$tmpl = $this->view;
		}
			
		// include template
		$tmplfile = "tmpl/$tmpl.tmpl.php";
		if (file_exists($tmplfile)) {
			
			// get tmpl result
			ob_start();
			include($tmplfile);
			$tmplcontent = ob_get_clean();
			ob_end_clean();
			
			// store in var
			$this->tmpl = $tmplcontent;
		} else {
			Main::error("No template found");
		}
	}
}

?>